/*
 * @Description: 分货计划DS
 * @Version: 1.0
 * @Autor: c-jack.qian
 * @Date: 2020-12-29 18:00:30
 * @LastEditors: c-jack.qian
 * @LastEditTime: 2021-04-08 16:37:08
 */

import { FieldType } from 'choerodon-ui/pro/lib/data-set/enum';
import commonConfigStore from '@common/config/commonConfig';
import moment from 'moment';
import intl from 'utils/intl';
import { resetMonth } from '../utils/utilsFun';

const O2_CM = process.env.NODE_ENV === 'development' ? '/o2cp' : commonConfigStore.O2_CM;
const O2_OM = process.env.NODE_ENV === 'development' ? '/o2om-c' : commonConfigStore.O2_OM;


const distributionPlanDS = () => ({
    autoQuery: true,
    selection: 'multiple',
    primaryKey: 'id',
    transport: {
        read: config => {
            const { data } = config;
            const { quotaMonth } = data;
            // 截掉月份后面的时间
            const submitData = resetMonth(data,quotaMonth,'quotaMonth')
            return {
                ...config,
                data: {
                    ...submitData,
                },
                url: `${O2_OM}/v1/quota-plans`,
                method: 'get',
            };
        },
        destroy: config => {
            return {
                ...config,
                url: `${O2_OM}/v1/quota-plans`,
                method: 'delete',
            };
        },
        exports: (config) => {
            return {
                ...config,
                url: `${O2_OM}v1/0/import/template/O2OM_C.QUOTA_PLAN/excel`,
                method: 'get',
            };
        },
    },
    fields: [
        {
            name: 'quotaMonth',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.quotaMonth').d('月份'),
            required: true,
        },
        {
            name: 'version',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.version').d('版本'),
            required: true,
        },
        {
            name: 'controlModeMeaning',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.controlModeCode').d('控制方式'),
            required: true,
        },
        {
            name: 'shortageTypeMeaning',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.shortageType').d('紧缺类型'),
            required: true,
        },
        {
            name: 'regionName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.region').d('大区'),
        },
        {
            name: 'areaName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.area').d('小区'),
        },
        {
            name: 'businessGroupMeaning',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.distributionPlan.groupName').d('组别'),
        },
        {
            name: 'customerName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.customerName').d('客户'),
        },
        {
            name: 'retailer',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.retailer').d('零售商'),
        },
        {
            name: 'productCategory',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.productCategory').d('产品类别'),
            required: true,
        },
        {
            name: 'productCode',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.productCode').d('料号'),
            required: true,
        },
        {
            name: 'productName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.productName').d('产品名称'),
            required: true,
        },
        {
            name: 'quantityDemand',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.quantityDemand').d('月要货量'),
            required: true,
        },
        {
            name: 'satisfactionRate',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.satisfactionRate').d('月满足率'),
        },
        {
            name: 'quantitySupply',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.quantitySupply').d('月给货量'),
        },
        {
            name: 'useQuantity',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.useQuantity').d('已使用量'),
        },
        {
            name: 'quantityFirstWeek',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.quantityFirstWeek').d('周计划给货量WK1'),
        },
        {
            name: 'quantitySecondWeek',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.quantitySecondWeek').d('周计划给货量WK2'),
        },
        {
            name: 'quantityThirdWeek',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.quantityThirdWeek').d('周计划给货量WK3'),
        },
        {
            name: 'quantityFourthWeek',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.quantityFourthWeek').d('周计划给货量WK4'),
        },
        {
            name: 'createdUserName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.createdUserName').d('创建人'),
        },
        {
            name: 'creationDate',
            type: FieldType.dateTime,
            label: intl.get('dji.otc.om.view.field.creationDate').d('创建时间'),
        },
        {
            name: 'planStatusCode',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.planStatusCode').d('计划状态'),
            lookupCode: 'DJISM.PLAN_STATUS',
        },
    ],
    queryFields: [
        {
            name: 'quotaMonth',
            type: FieldType.month,
            label: intl.get('dji.otc.om.view.field.quotaMonth').d('配额月份'),
            defaultValue: moment().format("YYYY MM"),
            required: true,
        },
        {
            name: 'controlModeCode',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.controlModeCode').d('控制方式'),
            lookupCode: 'DJISM.CONTROL_MODE',
            defaultValue: 'QUOTA',
            required: true,
        },
        {
            name: 'customerName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.customerName').d('客户'),
        },
        {
            name: 'productName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.productName').d('产品'),
        },
        // {
        //     name: 'region',
        //     type: FieldType.string,
        //     label: intl.get('dji.otc.om.view.field.tagType').d('大区'),
        //     lookupCode: 'DJIOM.CHECK_RULE_STATUS',
        // },
        // {
        //     name: 'area',
        //     type: FieldType.string,
        //     label: intl.get('dji.otc.om.view.field.tagType').d('小区'),
        //     lookupCode: 'DJIOM.CHECK_RULE_STATUS',
        // },
        {
            label: intl.get('dji.otc.om.view.field.region').d('大区'),
            labelWidth: 120,
            name: 'largeAreaObj',
            type: FieldType.object,
            checkValueOnOptionsChange: true,
            lookupUrl: `${O2_CM}/v1/cm-contract-permissions/get-largeArea-info`,
            ignore: 'always',
        },
        {
            label: intl.get('dji.otc.om.view.field.area').d('小区'),
            labelWidth: 120,
            name: 'areaObj',
            type: FieldType.object,
            checkValueOnOptionsChange: true,
            lookupUrl: `${O2_CM}/v1/cm-contract-permissions/get-area-info`,
            // cascadeMap: { parentCodeValue: 'largeAreaObj.value' },
            ignore: 'always',
            
            // dynamicProps: {
            //     disabled: ({ record }) => {
            //         if (!record.get('largeAreaObj')) {
            //             record.set('areaObj', null);
            //             return true;
            //         }
            //     },
            // },
        },
        {
            name: 'regionId',
            bind: 'largeAreaObj.value',
            type: FieldType.string,
        },
        {
            name: 'areaId',
            bind: 'areaObj.value',
            type: FieldType.string,
        },
        {
            name: 'customerType',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.customerType').d('客户类型'),
            lookupCode: 'DJISM.CUSTOMER_TYPE',
        },
        {
            name: 'retailer',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.retailer').d('零售商'),
        },
        {
            name: 'productCategory',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.productCategory').d('产品分类'),
        },
        {
            name: 'businessGroup',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.distributionPlan.groupName').d('组别'),
            lookupCode: 'DJICM_GROUP',
        },
        {
            name: 'version',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.version').d('版本号'),
        },
    ],
});

// 产销确认ds
const productMakeDS = () => ({
    autoCreate: true,
    dataToJSON: 'normal',
    transport: {
        submit: (config) => {
            return {
                ...config,
                data: config.data[0],
                url: `${O2_OM}/v1/quota-plans/confirm`,
                method: 'POST',
            };
        },
    },
    fields: [
        {
            name: 'quotaMonth',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.quotaMonth').d('月份'),
            required: true,
        },
        {
            name: 'version',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.version').d('版本'),
            required: true,
        },
        {
            name: 'controlModeCode ',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.controlModeCode').d('控制方式'),
            required: true,
        },
    ],
});


export { distributionPlanDS, productMakeDS };
